<template>
  <ContentWrap>
    <el-row class="w-20% mb-20px justify-center">
      <el-button
        v-hasPermi="['crm:module:update']"
        class="w-100%"
        plain
        type="primary"
        @click="openForm('create')"
      >
        <Icon icon="ep:plus" class="mr-5px" />
        新增跟进阶段
      </el-button>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" border>
      <el-table-column label="阶段名称" align="center" prop="name" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crm:module:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:module:update']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
  <!-- 新增模块form -->
  <Form ref="formRef" @success="getList"></Form>
</template>
<script setup lang="ts">
import Form from './form.vue'
import * as FollowStageApi from '@/api/crm/module/index'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(false)
const list = ref([])

const getList = async () => {
  try {
    loading.value = true
    const data = await FollowStageApi.getFollowStageConfigListDefault()
    if (data) {
      list.value = data
      loading.value = false
    }
  } catch {
    loading.value = false
  }
}

const formRef = ref()
const openForm = (type, id) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await FollowStageApi.deleteFollowStageConfigDefault(id)
    message.success('删除成功')
    // 刷新列表
    await getList()
  } catch {}
}

// 初始化
onMounted(() => {
  getList()
})
</script>
